package com.flux.ui.activity

import android.os.Build
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.ViewGroup

import com.flux.R
import kotlinx.android.synthetic.main.dp_base_activity_with_toolbar.*

/**
 * 基础ToolbarActivity
 * Created on 2017/8/30.
 *
 * @author Yang
 */
open class FBaseActivityWithToolbar : FBaseActivity() {
    lateinit var toolbar: Toolbar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        super.setContentView(R.layout.dp_base_activity_with_toolbar)
        toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        if (supportActionBar != null) supportActionBar!!.setDisplayShowTitleEnabled(false)
//        默认不显示搜索
        showTitleSearch(false, "")
    }

    fun showTitleSearch(isShow: Boolean, hint: String) {
        titleSearch.visibility = if (isShow) View.VISIBLE else View.GONE
        titleTextView.visibility = if (isShow) View.GONE else View.VISIBLE
        titleSearch.hint = hint
    }

    fun getSearchText() = titleSearch.text.toString()

    /**
     * 设置标题样式
     *
     * @param styleId 标题样式ID
     */
    fun setTitleTextAppearance(styleId: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) titleTextView!!.setTextAppearance(styleId)
        else  titleTextView!!.setTextAppearance(this, styleId)
    }

    /**
     * 设置标题
     *
     * @param titleResId 标题资源ID
     */
    override fun setTitle(titleResId: Int) {
        titleTextView!!.setText(titleResId)
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    fun setTitle(title: String) {
        titleTextView!!.text = title
    }

    override fun setContentView(layoutResID: Int) {
        setContentView(inflate(layoutResID))
    }

    override fun setContentView(view: View) {
        setContentView(view, null)
    }

    override fun setContentView(view: View, params: ViewGroup.LayoutParams?) {
        var param = params
        if (null == param) {
            param = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
        bodyContainer!!.addView(view, param)
    }
}